"""
目标：给定列表，返回二分查找成功元素的索引
示例：sequence=[1,3,5,...,995,997,999],target=521
"""


def binary_search(array, n):
    left = 0
    right = len(array) - 1
    while left <= right:
        mid = (left + right) // 2
        if array[mid] == n:
            return mid
        if array[mid] < n:
            left = mid
        if array[mid] > n:
            right = mid
    return -1


sequence = [2 * i + 1 for i in range(500)]
target = 521
print(binary_search(sequence, target))
